﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Input;
using SWCInvoice.ViewModel;
using SWCInvoice.Servicelayer;

namespace SWCInvoice.Model {
    public class Identifier : ModelBase {

        #region Fields

        /// <summary>
        /// Name of the identifier
        /// </summary>
        string _name;
        /// <summary>
        /// The string search keyword in the same line
        /// </summary>
        string _searchKeyword;
        /// <summary>
        /// The string search expression in the same line
        /// </summary>
        string _searchExpression;
        /// <summary>
        /// The regular expression
        /// </summary>
        string _expression;
        /// <summary>
        /// If evry page expect a own identifier. 
        /// If false the identifier would be inherited to the follwoing pages with no own identifier.
        /// If true (default) each page expect a own identifier.
        /// </summary>
        bool _inheritance;
        /// <summary>
        /// The filter can be executet as a additional filter after a other filter without delete the identifier. 
        /// Only pages with no identifier wher check agian.
        /// </summary>
        bool _additional;        
        /// <summary>
        /// The identifier group
        /// </summary>
        string _group;
        /// <summary>
        /// The identifier disable reason
        /// </summary>
        string _disableReason;
        /// <summary>
        /// The description of the identifier
        /// </summary>
        string _description;

        #endregion

        #region Properties

        /// <summary>
        /// The Name of the identifier
        /// </summary>
        public string Name {
            get {
                return _name;
            }
            set {
                if (_name == value)
                    return;
                _name = value;
                RaisePropertyChanged("Name");
            }
        }

        /// <summary>
        /// The string search keyword in the same line
        /// </summary>
        public string SearchKeyword {
            get {
                return _searchKeyword;
            }
            set {
                if (_searchKeyword == value)
                    return;
                _searchKeyword = value;
                RaisePropertyChanged("SearchKeyword");
            }
        }

        /// <summary>
        /// The string search expression in the same line
        /// </summary>
        public string SearchExpression {
            get {
                return _searchExpression;
            }
            set {
                if (_searchExpression == value)
                    return;
                _searchExpression = value;
                RaisePropertyChanged("SearchExpression");
            }
        }

        /// <summary>
        /// The regular expression
        /// </summary>
        public string Expression {
            get {
                return _expression;
            }
            set {
                if (_expression == value)
                    return;
                _expression = value;
                RaisePropertyChanged("Expression");
            }
        }

        /// <summary>
        /// If evry page expect a own identifier. 
        /// If false the identifier would be inherited to the follwoing pages with no own identifier.
        /// If true (default) each page expect a own identifier.
        /// </summary>
        public bool Inheritance {
            get {
                return _inheritance;
            }
            set {
                if (_inheritance == value)
                    return;
                _inheritance = value;
                RaisePropertyChanged("Inheritance");
            }
        }

        /// <summary>
        /// The filter can be executet as a additional filter after a other filter without delete the identifier. 
        /// Only pages with no identifier wher check agian.
        /// </summary>
        public bool Additional {
            get {
                return _additional;
            }
            set {
                if (_additional == value)
                    return;
                _additional = value;
                RaisePropertyChanged("Additional");
            }
        }

        /// <summary>
        /// The identifier group
        /// </summary>
        public string Group {
            get {
                return _group;
            }
            set {
                if (_group == value)
                    return;
                _group = value;
                RaisePropertyChanged("Group");
            }
        }

        /// <summary>
        /// The identifier disable reason
        /// </summary>
        public string DisableReason {
            get {
                return _disableReason;
            }
            set {
                if (_disableReason == value)
                    return;
                _disableReason = value;
                RaisePropertyChanged("DisableReason");
            }
        }

        /// <summary>
        /// The description of the identifier
        /// </summary>
        public string Discription {
            get {
                return _description;
            }
            set {
                if (_description == value)
                    return;
                _description = value;
                RaisePropertyChanged("Discription");
            }
        }

        /// <summary>
        /// The identifier helpText;
        /// </summary>
        public string HelpText {
            get {
                //&#x0a;
                
                string searchExpression = string.Empty;
                string searchKeyword    = string.Empty;
                string expression       = string.Empty;
                string inheritance      = string.Empty;
                string additional       = string.Empty;

                if (!string.IsNullOrWhiteSpace(_searchExpression))
                    searchExpression = string.Format("{0}\t\t{1}\r\n", Properties.ResourceMain.SearchExpression, _searchExpression);
                if (!string.IsNullOrWhiteSpace(_searchKeyword))
                    searchKeyword = string.Format("{0}\t{1}\r\n", Properties.ResourceMain.SearchKeyword, _searchKeyword);
                if (!string.IsNullOrWhiteSpace(_expression))
                    expression = string.Format("{0}\t\t{1}\r\n", Properties.ResourceMain.Expression, _expression);

                inheritance = string.Format("{0}\t\t{1}\r\n", Properties.ResourceMain.Inheritance, _inheritance.ToString());

                additional = string.Format("{0}\t\t{1}\r\n", Properties.ResourceMain.Additional, _additional.ToString());

                return string.Format("{0}{1}{2}{3}{4}\r\n{5}", searchExpression, searchKeyword, expression, inheritance, additional, _description);
            }          
        }

        #endregion

        #region Methods

        /// <summary>
        /// Creates a new identifier
        /// </summary>
        /// <param name="name">The name of the identifier</param>
        /// <param name="group">The identifier group</param>
        /// <param name="hook">The string hook in the same line</param>
        /// <param name="expression">The regular expression</param>
        /// <param name="inheritance">The inheritance state</param>
        /// <param name="additional">The additional state</param>
        /// <param name="description">The description of the filter</param>
        /// <returns>Identifier</returns>
        public static Identifier Create(string name, string group, string searchExpression, string searchKeyword, string expression, string inheritance, string additional, string description)
        {
            Identifier identifier = new Identifier();
            identifier._name = name;
            identifier._group = group;
            identifier._searchExpression = searchExpression;
            identifier._searchKeyword = searchKeyword;
            identifier._expression = expression;

            bool resultInheritance = false;
            bool.TryParse(inheritance, out resultInheritance);
            identifier._inheritance = resultInheritance;

            bool resultAdditional = false;
            bool.TryParse(additional, out resultAdditional);
            identifier._additional = resultAdditional;
            
            identifier._description = description;
            
            return identifier;
        }

        #endregion

        #region Collection

        public class IdentifierCollection : ObservableCollection<Identifier> { 
        
            }
        
        #endregion
    }
}
